package com.thinkcube.edutainment.fragment;

import java.util.List;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.Trace;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.rest.RestService;
import org.androidannotations.annotations.sharedpreferences.Pref;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.thinkcube.edutainment.EdutainmentConstant;
import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.ReachabilityTest;
import com.thinkcube.edutainment.adapters.MyCourseAdapter;
import com.thinkcube.edutainment.client.CourseClient;
import com.thinkcube.edutainment.client.UserClient;
import com.thinkcube.edutainment.domain.Course;
import com.thinkcube.edutainment.domain.OAuth;
import com.thinkcube.edutainment.domain.data.CourseData;
import com.thinkcube.edutainment.domain.dto.CourseDto;
import com.thinkcube.edutainment.domain.dto.OAuthDto;
import com.thinkcube.edutainment.domain.dto.UserOAthDto;
import com.thinkcube.edutainment.service.SPreferences_;

@EFragment(R.layout.mycourse)
public class MyCoureseDashbordCourse extends Fragment implements
		OnItemClickListener {

	@ViewById(R.id.mycourselist)
	ListView myCourseList;

	@ViewById(R.id.progressBar)
	ProgressBar progress;
	
	@ViewById(R.id.not_found)
	TextView notFound;

	@RestService
	CourseClient courseClient;

	@Bean
	CourseDto courseDto;

	@RestService
	UserClient userClient;

	@Bean
	OAuthDto oAuthDto;

	@Trace
	@AfterViews
	@Background
	void gettoken() {
		oAuthDto.setAppId(EdutainmentConstant.APP_ID);
		oAuthDto.setAppSecret(EdutainmentConstant.APP_SCRET);
		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					setTokenUi(userClient.getRequestToken(oAuthDto
							.getMultiValuedMap()));
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("Not connected ");
			}
		});

	}

	@UiThread
	void showMessge(final String msg) {
		Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
	}

	void setTokenUi(OAuth requestToken) {

		doBackground(requestToken);
	}

	@Bean
	UserOAthDto userOAthDto;

	@Pref
	SPreferences_ pref;

	@Background
	void doBackground(OAuth requestToken) {

		userOAthDto.setAuthToken(requestToken.getoAuth().getToken());
		userOAthDto.setUserId(pref.getId().get());
		//userOAthDto.setUserId("5211a3156bf8411a5adf1a01");

		new ReachabilityTest(getActivity(), new ReachabilityTest.Callback() {

			@Override
			public void onReachabilityTestPassed() {

				try {
					doUi(courseClient.getGetMyCourses(userOAthDto
							.getMultiValuedMap()));
				} catch (Exception e) {

					System.err.println(e);
				}
			}

			@Override
			public void onReachabilityTestFailed() {

				showMessge("Not connected ");
			}
		});
	}

	@Bean
	MyCourseAdapter myCourseAdapter;

	private List<CourseData> courseList;

	@Trace
	@UiThread
	void doUi(Course course) {

		if (course.getCourseList() != null) {
			courseList = course.getCourseList();
			myCourseAdapter.setCourseDataList(courseList);

			myCourseList.setAdapter(myCourseAdapter);
			myCourseList.setOnItemClickListener(this);
		}
		else
		{
			notFound.setVisibility(View.VISIBLE);
		}
		

		progress.setVisibility(View.GONE);

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

		FragmentTransaction fragmentTransaction = getFragmentManager()
				.beginTransaction();
		MyCourseDetailDashBoard coursWork = new MyCourseDetailDashBoard_();
		Bundle bundle = new Bundle();
		bundle.putString("key", "mycourse");
		bundle.putParcelable("course", courseList.get(arg2));
		coursWork.setArguments(bundle);

		fragmentTransaction.replace(R.id.pane, coursWork);
		fragmentTransaction.addToBackStack(null);
		fragmentTransaction.commit();

	}
	


}
